import { defineStore } from "pinia";
import { getOrganizationalInfo } from "@/api/organization";
import { pinyin } from "pinyin-pro";
const init = {};

export const useOrganizationStore = defineStore("organization", {
  state: () => ({ ...init }),
  actions: {
    async getInfo() {
      const {
        data: { organizational },
      } = await getOrganizationalInfo();
      const groupMembersIndexList = [];
      const groupMembersItemArr = [];
      organizational.groupMembers.sort((a, b) => {
        return a.name.localeCompare(b.name);
      });
      organizational.groupMembers.forEach((item, index) => {
        const first = pinyin(item.name, { pattern: "first", type: "array" })[0];
        groupMembersIndexList.push(first);
        item["title"] = `${item.name} | ${item.rateDepartment} | ${item.position.replace(item.rateDepartment, "")}`;
        groupMembersItemArr.push(item);
      });
      return { ...organizational, groupMembersIndexList, groupMembersItemArr };
    },
  },
  persist: {
    enabled: true,
    detached: true, // 设置订阅与组件分离
  },
});
